package com.aho.app.yourfancy;

import java.text.DecimalFormat;

import com.aho.app.util.InterpolateAnimation;

import android.R.drawable;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.DecelerateInterpolator;

public class MyRatingView extends View implements OnGestureListener {

	private static final int MSG_UPDATE = 0;
	
	private MainActivity mMain = null;
	private GestureDetector mGestureDetector = null;
	private TimerHandler mTimer = null;
	private int mPositiveCount = 0;
	private int mNegativeCount = 0;
	private DrawObject mGoodObject = null;
	private DrawObject mBadObject = null;
	private int mButtonSize = 0;;
	private InterpolateAnimation mShowPressAnim = null;
	
	public MyRatingView(Context context) {
		super(context);
		
		mMain = (MainActivity)context;
		mTimer = new TimerHandler(this);
		mGestureDetector = new GestureDetector(this);
		
		mButtonSize = Math.round(mMain.getWidth() / 3.0f);
		
		setBackgroundResource(R.drawable.bg);
		
		Resources r = mMain.getResources();
		Bitmap goodBm = BitmapFactory.decodeResource(r, R.drawable.good);
		mGoodObject = new DrawObject();
		mGoodObject.init(goodBm, Math.round(mMain.getWidth() / 4.0f), Math.round(mMain.getHeight() / 3.0f), mButtonSize, mButtonSize);
		Bitmap badBm = BitmapFactory.decodeResource(r, R.drawable.bad);
		mBadObject = new DrawObject();
		mBadObject.init(badBm, Math.round(3.0f * mMain.getWidth() / 4.0f), Math.round(mMain.getHeight() / 3.0f), mButtonSize, mButtonSize);
		
		mShowPressAnim = new InterpolateAnimation(1.0f, 1.25f, 200, new DecelerateInterpolator());
		
		mTimer.sendEmptyMessage(MSG_UPDATE);
	}

	public void update() {
		invalidate();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		if (mShowPressAnim.isPlaying()) {
			final float size = mButtonSize * mShowPressAnim.interpolate();
			switch (mHitObjectIndex) {
				case (HIT_TO_POSITIVE) : {
					mGoodObject.setSize(Math.round(size), Math.round(size));
					mGoodObject.setCenterXY(Math.round(mMain.getWidth() / 4.0f), Math.round(mMain.getHeight() / 3.0f));
					break;
				}
				case (HIT_TO_NEGATIVE) : {
					mBadObject.setSize(Math.round(size), Math.round(size));
					mBadObject.setCenterXY(Math.round(3.0f * mMain.getWidth() / 4.0f), Math.round(mMain.getHeight() / 3.0f));
					break;
				}
				case (HIT_DISABLED) : {
					break;
				}
				default : {
					break;
				}
			}
		}
	 	
		Paint p = new Paint();
		p.setAntiAlias(true);
		p.setTextSize(mMain.getWidth() / 18.0f);
		p.setColor(Color.WHITE);
		canvas.drawText("あなたの近くの異性をどう思いますか？", 0, mMain.getHeight() / 15.0f, p);
		p.setColor(Color.CYAN);
		p.setTextSize(mMain.getWidth() / 10.0f);
		canvas.drawText("Good!!", mMain.getWidth() / 10.0f, mMain.getHeight() / 5.0f, p);
		p.setColor(Color.MAGENTA);
		canvas.drawText("Bad!!", mMain.getWidth() * 6.0f / 10.0f, mMain.getHeight() / 5.0f, p);
		p.setColor(Color.GREEN);
		canvas.drawText(Integer.toString(mPositiveCount), mMain.getWidth() / 4.0f, mMain.getHeight() / 2.0f, p);
		canvas.drawText(Integer.toString(mNegativeCount), 3.0f * mMain.getWidth() / 4.0f, mMain.getHeight() / 2.0f, p);
		
		float ave = (float)mPositiveCount / ((float)mPositiveCount + (float)mNegativeCount);
        DecimalFormat df = new DecimalFormat();
        df.applyPattern("0");
        df.setMaximumFractionDigits(3);
        df.setMinimumFractionDigits(3);
        Float objnum = new Float(ave);
		//String aveStr = "Average = " + String.format("%0.3f", ave);
        String aveStr = "Average : " + df.format(objnum);
		canvas.drawText(aveStr, mMain.getWidth() / 5.0f, mMain.getHeight() * 3.0f / 5.0f, p);
		
		mGoodObject.draw(canvas);
		mBadObject.draw(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mGestureDetector.onTouchEvent(event);
		if (event.getAction() == MotionEvent.ACTION_UP) {
			mGoodObject.setSize(mButtonSize, mButtonSize);
			mGoodObject.setCenterXY(Math.round(mMain.getWidth() / 4.0f), Math.round(mMain.getHeight() / 3.0f));
			mBadObject.setSize(mButtonSize, mButtonSize);
			mBadObject.setCenterXY(Math.round(3.0f * mMain.getWidth() / 4.0f), Math.round(mMain.getHeight() / 3.0f));
			mHitObjectIndex = HIT_DISABLED;
		}
		return true;
	}

	private static final int HIT_DISABLED = 0;
	private static final int HIT_TO_POSITIVE = 1;
	private static final int HIT_TO_NEGATIVE = 2;
	private int mHitObjectIndex = HIT_DISABLED;
	
	public boolean onDown(MotionEvent e) {
		if (!mShowPressAnim.isPlaying()) {
			if (mGoodObject.hitTestRect((int)e.getX(), (int)e.getY())) {
				mShowPressAnim.start();
				mHitObjectIndex = HIT_TO_POSITIVE;
			} else if (mBadObject.hitTestRect((int)e.getX(), (int)e.getY())){
				mShowPressAnim.start();
				mHitObjectIndex = HIT_TO_NEGATIVE;
			} else {
				mHitObjectIndex = HIT_DISABLED;
			}
		}
		return true;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return false;
	}

	public void onLongPress(MotionEvent e) {
		
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	public void onShowPress(MotionEvent e) {
		
	}

	public boolean onSingleTapUp(MotionEvent e) {
		if (mGoodObject.hitTestRect((int)e.getX(), (int)e.getY())) {
			doCountUpAction();
		} else if (mBadObject.hitTestRect((int)e.getX(), (int)e.getY())){
			doCountDownAction();
		}
		return true;
	}

	public void doCountUpAction() {
		mPositiveCount++;
		invalidate();
	}

	public void doCountDownAction() {
		mNegativeCount++;
		invalidate();
	}
	
	public int getPosCount(){
		return mPositiveCount;
	}
	
	public int getNegCount(){
		return mNegativeCount;
	}
	
//	public void setPosCount(int pos){
//		mPositiveCount = pos;
//		invalidate();
//	}
//	
//	public void setNegCount(int neg){
//		mNegativeCount = neg;
//		invalidate();
//	}
	
	public void setRating(Rating rating){
		mPositiveCount = rating.getPosCount();
		mNegativeCount = rating.getNegCount();
		invalidate();
	}
	
	private class TimerHandler extends Handler {
		
		private MyRatingView mParent;
		
		public TimerHandler(MyRatingView parentIn) {
			mParent = parentIn;
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case (MSG_UPDATE) : {
					mParent.update();
					removeMessages(MSG_UPDATE);
					sendEmptyMessageDelayed(MSG_UPDATE, 33);
					break;
				}
				default : {
					break;
				}
			}
		}
		
			
	}

}
